/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.transfer;

import com.wtkj.vo.dzzf.base.kfwd.Account;
import com.wtkj.vo.dzzf.base.kfwd.FundAccount;
import com.wtkj.vo.dzzf.share.staticvalue.CardStaticValue;
import com.wtkj.vo.dzzf.share.staticvalue.CertifiType;
import com.wttech.dzzf.process.dao.DataTransferDao;
import com.wttech.sjhj.check.moduleSet.CheckUserUpload_8;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

import static com.wttech.dzzf.process.constant.UserConstants.*;


/**
 * 数据迁移 - 资金账户检测服务实现
 *
 * @author MaShibo
 * @date 2017/11/6.
 */
@Service
public class FundAccountCheckService {

    private static final Logger LOGGER = LoggerFactory.getLogger(FundAccountCheckService.class);

    private static final Logger ERRORINFO = DataTransferService.ERRORINFO;

    /**
     * 建行默认TOPCLASS
     */
    static final String DEFAULT_TOPCLASS = "6105";

    /**
     * 经办人证件号
     */
    private static final String AGENTIDNUM_STR = "agentIdNum";

    @Resource
    private DataTransferDao dataTransferDao;

    @Resource
    private AccountCheckService accountCheckService;

    void analyseFundAccount(FundAccount fundAccount) {

        //检查必填字段是否为空
        checkFundAccountInfosIsValid(fundAccount);

        //检查ID是否重复
        String fundAcountId = fundAccount.getFundAccountId();
        Integer idNum = dataTransferDao.queryFundAccountId(fundAcountId);
        if (idNum != null && idNum >= 1) {
            ERRORINFO.info("资金账户{}，ID【{}】错误", fundAcountId, fundAcountId);
        }

        //检查账户号是否重复
        String fundAccountNo = fundAccount.getFundAccountNo();
        Integer noNum = dataTransferDao.queryFundAccountNo(fundAccountNo);
        if (noNum != null && noNum >= 1) {
            ERRORINFO.info("资金账户{}，ID【{}】错误", fundAcountId, fundAccountNo);
        }

        //查询并分析业务账户信息
        List<Account> accounts = dataTransferDao.queryAccountInfosByFundAccountId(fundAcountId);
        if (CollectionUtils.isEmpty(accounts)) {
            ERRORINFO.info("资金账户{}，不存在对应的业务账户信息", fundAcountId);
        } else {
            accounts.forEach(account -> accountCheckService.analyseAccount(account));
        }
    }

    /**
     * 检查资金账户信息是否有效
     *
     * @param fundAccount 资金账户信息
     */
    private void checkFundAccountInfosIsValid(FundAccount fundAccount) {

        String fundAccountId = fundAccount.getFundAccountId();
        //账户类型
        Integer ecardType = fundAccount.getAccountType();
        if (ecardType == null || (!ecardType.equals(CardStaticValue.EcardType.CARD_TYPE_CZ) && !ecardType.equals(CardStaticValue.EcardType.CARD_TYPE_JZ))) {
            ERRORINFO.info("资金账户{}，账户类型【{}】错误", fundAccountId, ecardType);
        }
        //地址
        String address = fundAccount.getAddress();
        if (!StringUtils.equalsIgnoreCase(CheckUserUpload_8.checkAddress(address), RIGHT)) {
            ERRORINFO.info("资金账户{}, 地址【{}】格式错误", fundAccountId, address);
        }
        //证件类型
        Integer certifiType = fundAccount.getCertifiType();
        if (!CertifiType.CertifiTypeMap.containsKey(certifiType)) {
            ERRORINFO.info("资金账户{}，证件类型【{}】错误", fundAccountId, certifiType);
        }
        //用户类型
        Integer ownerType = fundAccount.getOwnerType();
        if (ownerType == null || (!ownerType.equals(OLD_OWNERTYPE_USER) && !ownerType.equals(OLD_OWNERTYPE_COMPANY))) {
            ERRORINFO.info("资金账户{}，用户类型【{}】错误", fundAccountId, ownerType);
        }
        //手机号
        String mobile = fundAccount.getMobile();
        if (!StringUtils.equalsIgnoreCase(CheckUserUpload_8.checkTel(mobile), RIGHT)) {
            ERRORINFO.info("资金账户{}， 手机号【{}】错误", fundAccountId, mobile);
        }
        //TOPCLASS
        String topClass = fundAccount.getTopClass();
        if (!StringUtils.equals(DEFAULT_TOPCLASS, topClass)) {
            ERRORINFO.info("资金账户{}，TOPCLASS【{}】错误", fundAccountId, topClass);
        }
        //经办人信息
        if (ownerType != null && ownerType == OLD_OWNERTYPE_COMPANY) {
            //经办人证件类型
            Integer agentIdType = fundAccount.getAgentIdType();
            if (!CertifiType.CertifiTypeMap.containsKey(agentIdType) || agentIdType == CertifiType.ORGCODE) {
                ERRORINFO.info("资金账户{}，经办人证件类型【{}】错误", fundAccountId, agentIdType);
            }
            //经办人证件号
            String agentIdNum = fundAccount.getAgentIdNum();
            try {
                if (!StringUtils.equalsIgnoreCase(CheckUserUpload_8.checkUserUpload(AGENTIDNUM_STR, agentIdNum), RIGHT)) {
                    ERRORINFO.info("资金账户{}，经办人证件号【{}】错误", fundAccountId, agentIdNum);
                }
            } catch (ParseException e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
    }
}
